import { defineStore } from 'pinia'
import { Ref, ref } from "vue";
import Cookies from 'js-cookie';
export const useStore = defineStore('user', () => {
  // 用户令牌
  const token: Ref<string> = ref('');
  // 用户id
  const uid: Ref<string> = ref('');
  // 用户名
  const userinfo: Ref<any> = ref({});
  // 用户名
  const username: Ref<string> = ref('');
  // 用户头像
  const head_img: Ref<string> = ref('');
  //是否超级管理员
  const isSuperAdmin: Ref<boolean> = ref(false);
  // //渠道id
  // const channel_id: Ref<any> = ref(0);
  interface loginInfoStorageInterface {
    token: string,
    uid: string,
    head_img: string,
    username: string,
    isSuperAdmin: boolean,
  }
  // 登录
  const login = (loginInfo: loginInfoStorageInterface) => {
    token.value = loginInfo.token || '';
    uid.value = loginInfo.uid || '';
    head_img.value = loginInfo.head_img || '';
    username.value = loginInfo.username || '';
    isSuperAdmin.value = loginInfo.isSuperAdmin || false;
  }
  const logout = () => {
    token.value = '';
    uid.value = '';
    username.value = '';
    head_img.value = '';
  }
  return {
    token,
    uid,
    username,
    head_img,
    isSuperAdmin,
    login,
    logout,
  }
}, {
  persist: {
    enabled: true,
    strategies: [{
      key: process.env.VUE_APP_ENV + "user",
      storage: {
        getItem: (key: string) => Cookies.get(key),
        setItem: (key: string, val: any) => Cookies.set(key, val),
        removeItem: (key: string) => Cookies.remove(key),
      },
    }]
  }
} as any)
